﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace PrintBarcode.BUS
{
   
    public class TStampAll
    {
        UTLDB.UTLDB connect = new UTLDB.UTLDB();
        DataTable data = new DataTable();
        DataTable dataSQL= new DataTable();
        public TStampAll() {
            DataColumn[] dc = new DataColumn[]{ 
                                new DataColumn("T11", Type.GetType("System.String")),
                                new DataColumn("T12", Type.GetType("System.String")),
                                new DataColumn("T21", Type.GetType("System.String")),
                                new DataColumn("T22", Type.GetType("System.String")),
                                new DataColumn("T31", Type.GetType("System.String")),
                                new DataColumn("T32", Type.GetType("System.String")),
                                new DataColumn("T41", Type.GetType("System.String")),
                                new DataColumn("T42", Type.GetType("System.String")),
                                new DataColumn("T51", Type.GetType("System.String")),
                                new DataColumn("T52", Type.GetType("System.String")),
                                new DataColumn("Barcode", Type.GetType("System.String")),
                                new DataColumn("lable", Type.GetType("System.String"))

            };
            data.Columns.AddRange(dc);

        }
        public DataRow Get_DoThiet(int i) {
             DataRow  r = data.NewRow();
             r["T11"] = "TL:";
             r["T51"] = "C:";
                if (dataSQL.Rows[i]["UnitID"].ToString() == "1")
                {
                    r["T12"] = dataSQL.Rows[i]["Weight"].ToString() + "Gam";
                    r["T52"] = String.Format("{0:0}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()) + float.Parse(dataSQL.Rows[i]["giahatlon"].ToString()) + float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
                }
                else {
                    r["T12"] = FormatUnit(dataSQL.Rows[i]["Weight"].ToString());
                    r["T52"] = dataSQL.Rows[i]["TienCong"].ToString();
            
                }
                r["T21"] = dataSQL.Rows[i]["hatlon"].ToString();
                r["T22"] = dataSQL.Rows[i]["giahatlon"].ToString();
                r["T31"] = dataSQL.Rows[i]["hattam"].ToString();
                r["T32"] = dataSQL.Rows[i]["giahattam"].ToString();
                r["T41"] = "V";
                r["T42"] = String.Format("{0:0}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()));
                r["BarCode"] = dataSQL.Rows[i]["Barcode"].ToString().Trim();
                r["Lable"] = dataSQL.Rows[i]["Barcode"].ToString();
               // r["NameUnit"] = dt.Rows[i]["NameUnit"].ToString();

                return r;
        
        }
        public DataRow Get_DoThuong(int i)
        {
            DataRow r = data.NewRow();
            r["T11"] = "TL:";
           // r["T51"] = "C:";
            if (dataSQL.Rows[i]["UnitID"].ToString() == "1")
            {
                r["T12"] = dataSQL.Rows[i]["WSheed"].ToString() + "Gam";
             //   r["T52"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()) + float.Parse(dataSQL.Rows[i]["giahatlon"].ToString()) + float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
            }
            else
            {
                r["T12"] = FormatUnit(dataSQL.Rows[i]["WSheed"].ToString());
               // r["T52"] = dataSQL.Rows[i]["TienCong"].ToString();

            }
            r["T21"] = "H:" + dataSQL.Rows[i]["qualitytam"].ToString();
            r["T22"] = dataSQL.Rows[i]["giahatlon"].ToString();
           // r["T31"] = dataSQL.Rows[i]["hattam"].ToString();
           // r["T32"] = dataSQL.Rows[i]["giahattam"].ToString();
            r["T31"] = dataSQL.Rows[i]["Source"].ToString();
           // r["T42"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()));
            r["BarCode"] = dataSQL.Rows[i]["Barcode"].ToString().Trim();
            r["Lable"] = dataSQL.Rows[i]["Barcode"].ToString();
            // r["NameUnit"] = dt.Rows[i]["NameUnit"].ToString();

            return r;

        }
        public DataRow Get_Vang18k(int i)
        {
            DataRow r = data.NewRow();
            r["T11"] = "TL:";
            // r["T51"] = "C:";
            if (dataSQL.Rows[i]["UnitID"].ToString() == "1")
            {
                r["T12"] = dataSQL.Rows[i]["WSheed"].ToString() + "Gam";
                //   r["T52"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()) + float.Parse(dataSQL.Rows[i]["giahatlon"].ToString()) + float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
            }
            else
            {
                r["T12"] = FormatUnit(dataSQL.Rows[i]["WSheed"].ToString());
                // r["T52"] = dataSQL.Rows[i]["TienCong"].ToString();

            }
            r["T21"] = "H:" + dataSQL.Rows[i]["qualitytam"].ToString();
            r["T22"] = dataSQL.Rows[i]["giahatlon"].ToString();
            r["T31"] = "V:" + dataSQL.Rows[i]["GoldType"].ToString();
           //  r["T32"] = dataSQL.Rows[i]["GoldType"].ToString();
            r["T41"] = "C";
            r["T42"] = String.Format("{0:0}", float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
            r["BarCode"] =dataSQL.Rows[i]["Barcode"].ToString().Trim();
            r["Lable"] = dataSQL.Rows[i]["Barcode"].ToString();
            // r["NameUnit"] = dt.Rows[i]["NameUnit"].ToString();

            return r;

        }      
        public DataTable GetStamp()
        {
            DataTable data = connect.GetDataByStoredProcedure("sp_mavach");
            return data;
        }
        public DataTable GetStamp(string barCode)
        {
            DataTable data = connect.GetDataByStoredProcedure("[sp_mavachByBar]",
                           new string[] { "@Bar" },  new object[] {barCode});
            return data;
        }
        public DataTable GetStampArrBar(string arrbarCode)
        {
            DataTable data = connect.GetDataByStoredProcedure("[sp_mavach_ArrBar]",
                           new string[] { "@arrbarcode" }, new object[] { arrbarCode });
            return data;
        }
        public string FormatUnit(string str)
        {

            string result = "";
            string[] temp = str.Trim().Split('.');
            int len = temp[0].Length;
            switch (len)
            {
                case 1:
                    result = temp[0] + "L";
                    break;
                case 2:
                    result = temp[0].Substring(0, 1) + "P" + temp[0].Substring(1, 1) + "L";
                    break;
                case 3:
                    result = temp[0].Substring(0, 1) + "C" + temp[0].Substring(1, 1) + "P" + temp[0].Substring(2, 1) + "L";
                    break;
                case 4:
                    result = temp[0].Substring(0, 1) + "L" + temp[0].Substring(1, 1) + "C" + temp[0].Substring(2, 1) + "P" + temp[0].Substring(3, 1) + "L";
                    break;
            }
            if (temp.Length == 2)
                result += temp[1];
            return result;
        }
        public DataTable SetData()
        {
            DataRow r;
            int i = 0;
            dataSQL = GetStamp();

            for (i = 0; i < dataSQL.Rows.Count; i++)
            {
                if (dataSQL.Rows[i]["IDType"].ToString()=="2")
                 r = Get_DoThuong(i);
                else if (dataSQL.Rows[i]["IDType"].ToString() == "5")
                 r=  Get_Vang18k(i);
                else
                    r = Get_DoThiet(i);
                data.Rows.Add(r);
            }
            return data;
        }
        public DataTable SetData(string bar)
        {
            DataRow r;
            int i = 0;
            dataSQL = GetStamp(bar);
            if (dataSQL.Rows.Count >0)
            {
                for (i = 0; i < dataSQL.Rows.Count; i++)
                {
                    if (dataSQL.Rows[i]["IDType"].ToString() == "2")
                        r = Get_DoThuong(i);
                    else if (dataSQL.Rows[i]["IDType"].ToString() == "5")
                        r = Get_Vang18k(i);
                    else
                        r = Get_DoThiet(i);
                    data.Rows.Add(r);
                }
            }
            return data;
        }
        public DataTable SetDataArrBarCode(string arrbar)
        {
            DataRow r;
            int i = 0;
            dataSQL = GetStampArrBar(arrbar);
            if (dataSQL.Rows.Count > 0)
            {
                for (i = 0; i < dataSQL.Rows.Count; i++)
                {
                    if (dataSQL.Rows[i]["IDType"].ToString() == "2")
                        r = Get_DoThuong(i);
                    else if (dataSQL.Rows[i]["IDType"].ToString() == "5")
                        r = Get_Vang18k(i);
                    else
                        r = Get_DoThiet(i);
                    data.Rows.Add(r);
                }
            }
            return data;
        }
        public bool UpdateProduct(string id,int status) { 
            try{
                connect.ExecuteNonQuery("[sp_UpdateStatictblProduct]",
                           new string[] { "@Barcode","@Status" },
                new object[] { id, status });
            return true;
            } catch(Exception e ){
                Console.Write(e.Message);
                return false;
            }

        }
        public bool UpdateProduct_arr(string arrid, int status)
        {
            try
            {
                connect.ExecuteNonQuery("[sp_UpdateStatictblProduct_arr]",
                           new string[] { "@arrBarcode", "@Status" },
                new object[] { arrid, status });
                return true;
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                return false;
            }

        }
    
    }

}
